﻿using System;
using System.Linq;

namespace Ji.DataHelper
{
    public static class TypeHelper
    {
        /// <summary> 获取指定类型的指定属性 </summary>
        /// <typeparam name="T"></typeparam>
        /// <typeparam name="R"></typeparam>
        /// <param name="data"></param>
        /// <param name="name"></param>
        /// <returns></returns>
        public static R GetAttribute<T, R>(this T data, string name) where R : Attribute
        {
            return (from r in (from i in data.GetType().GetFields() where i.Name.Equals(name) select i) let ca = r.GetCustomAttributes(true).FirstOrDefault(c => (c as R) != null) where (ca != null) select ca).FirstOrDefault() as R;
        }
    }
}